﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace WikiQuizz.Common.Utils
{
    public class Workbench
    {
        public static void SerializeToFile(object entity, string file)
        {
            StreamWriter writer = null;

            try
            {
                XmlSerializer serializer = new XmlSerializer(entity.GetType());

                writer = new StreamWriter(file, false, Encoding.UTF8);
                serializer.Serialize(writer, entity);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }

        public static T DeSerializeFromFile<T>(string file)
        {
            StreamReader reader = null;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));

                reader = new StreamReader(file, Encoding.UTF8);

                return (T)serializer.Deserialize(reader);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }        

    }
}
